<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="资讯标题" prop="newsTitle">
      <el-input v-model="dataForm.newsTitle" placeholder="资讯标题"></el-input>
    </el-form-item>
    <el-form-item label="作者名" prop="authorName">
      <el-input v-model="dataForm.authorName" placeholder="作者名"></el-input>
    </el-form-item>
    <el-form-item label="头像url" prop="headerUrl">
      <el-input v-model="dataForm.headerUrl" placeholder="头像url"></el-input>
    </el-form-item>
    <el-form-item label="评论数" prop="commentCount">
      <el-input v-model="dataForm.commentCount" placeholder="评论数"></el-input>
    </el-form-item>
    <el-form-item label="发布日期" prop="releaseDate">
      <el-input v-model="dataForm.releaseDate" placeholder="发布日期"></el-input>
    </el-form-item>
    <el-form-item label="资讯显示类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="资讯显示类型"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          newsId: 0,
          newsTitle: '',
          authorName: '',
          headerUrl: '',
          commentCount: '',
          releaseDate: '',
          type: ''
        },
        dataRule: {
          newsTitle: [
            { required: true, message: '资讯标题不能为空', trigger: 'blur' }
          ],
          authorName: [
            { required: true, message: '作者名不能为空', trigger: 'blur' }
          ],
          headerUrl: [
            { required: true, message: '头像url不能为空', trigger: 'blur' }
          ],
          commentCount: [
            { required: true, message: '评论数不能为空', trigger: 'blur' }
          ],
          releaseDate: [
            { required: true, message: '发布日期不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '资讯显示类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.newsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.newsId) {
            this.$http({
              url: this.$http.adornUrl(`/app/news/info/${this.dataForm.newsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.newsTitle = data.news.newsTitle
                this.dataForm.authorName = data.news.authorName
                this.dataForm.headerUrl = data.news.headerUrl
                this.dataForm.commentCount = data.news.commentCount
                this.dataForm.releaseDate = data.news.releaseDate
                this.dataForm.type = data.news.type
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/news/${!this.dataForm.newsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'newsId': this.dataForm.newsId || undefined,
                'newsTitle': this.dataForm.newsTitle,
                'authorName': this.dataForm.authorName,
                'headerUrl': this.dataForm.headerUrl,
                'commentCount': this.dataForm.commentCount,
                'releaseDate': this.dataForm.releaseDate,
                'type': this.dataForm.type
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
